package org.derusse.cycle;

import org.derusse.cycle.activity.CycleMeterActivity;
import org.derusse.cycle.database.DatabaseHelper;
import org.derusse.cycle.model.Track;
import org.derusse.cycle.service.CycleMeterService;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.widget.Toast;

public class CycleMeterApplication extends Application {

	private Track currentTrack;
	public long trackView = 0;
	private LocationManager locationManager;
	private DatabaseHelper databaseHelper;
	private NotificationManager notificationManager;

	private Intent intent;
	
	public LocationManager getLocationManager() {
		if (this.locationManager == null) {
			this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		}
		return this.locationManager;
	}
	public DatabaseHelper getDatabaseHelper() {
		if (this.databaseHelper == null) {
			this.databaseHelper = new DatabaseHelper(this);
		}
		return this.databaseHelper;
	}
	public NotificationManager getNotificationManager() {
		if (this.notificationManager == null) {
			this.notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		}
		return this.notificationManager;
	}
	public Track getTrack() {
		if (this.currentTrack == null) {
			this.currentTrack = new Track(this);
		}
		return this.currentTrack;
	}

	public void StartTrack()
	{
		if(!this.checkUnrunning())
		{
			return;
		}
		this.intent = new Intent(this, CycleMeterService.class);
		startService(this.intent);
		Notify(R.string.notify_track_start, R.drawable.icon_notify_run);
	}
	
	public void PauseTrack()
	{
		if(!this.checkRunning())
		{
			return;
		}
		this.currentTrack.Pause();
		Toast.makeText(getBaseContext(),
				getString(R.string.toast_track_pause), Toast.LENGTH_SHORT)
				.show();
		Notify(R.string.notify_track_pause, R.drawable.icon_notify_pause);
	}
	
	public void ResumeTrack()
	{
		if(!this.checkRunning())
		{
			return;
		}
		this.currentTrack.Resume();
		Notify(R.string.notify_track_resume, R.drawable.icon_notify_run);
	}

	public void StopTrack() {
		if(this.intent == null) return;
		stopService(this.intent);
		this.intent = null;
		this.getNotificationManager().cancel(R.string.notify_track);
		Toast.makeText(getBaseContext(),
				getString(R.string.toast_track_stop), Toast.LENGTH_SHORT)
				.show();
	}
	
	public void Notify(int idMessage, int idIcon)
	{
		Notification notification = new Notification();
		notification.when = System.currentTimeMillis();
		notification.icon = idIcon;
		notification.flags = Notification.FLAG_ONGOING_EVENT;
		notification.tickerText = getString(idMessage);

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, CycleMeterActivity.class), 0);

		notification.setLatestEventInfo(this, getText(R.string.app_name),
				getString(R.string.notify_track), contentIntent);
		this.getNotificationManager().notify(R.string.notify_track,
				notification);

	}
	
	private boolean checkRunning()
	{
		if(this.intent == null)
		{
			Toast.makeText(getBaseContext(),
					getString(R.string.toast_error_track_unexists), Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		return true;
	}
	private boolean checkUnrunning()
	{
		if(this.intent != null)
		{
			Toast.makeText(getBaseContext(),
					getString(R.string.toast_error_track_exists), Toast.LENGTH_SHORT)
					.show();
			return false;
		}
		return true;
	}
}